<template>
  <div class="dialog-form">
    <el-form ref="PlatCodeManagementRef" :model="form" :rules="rules" label-width="100px">
      <el-row :gutter="36">
        <el-col :span="12">
          <el-form-item label="码方案名称" prop="codeName">
            <el-input v-model="form.codeName" placeholder="请输入码方案名称" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="码方案code" prop="codeLabel">
            <el-input v-model="form.codeLabel" placeholder="限英文、下划线和数字" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="码类别" prop="codeType">
            <el-select v-model="form.codeType" placeholder="请选择码类别">
              <el-option
                v-for="dict in bus_qr_code_type"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="码标准" prop="codeStandard">
            <el-select v-model="form.codeStandard" placeholder="请选择码标准">
              <el-option
                v-for="dict in bus_qr_code_standard"
                :key="dict.value"
                :label="dict.label"
                :value="parseInt(dict.value)"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="发码机构" prop="codeOrgan">
            <el-input v-model="form.codeOrgan" placeholder="请输入发码机构" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="公钥证书" prop="caId">
            <el-select v-model="form.caId" placeholder="请选择">
              <el-option 
                v-for="dict in caList" 
                :key="dict.id" 
                :label="dict.requestNum" 
                :value="dict.id" 
                :disabled="dict.enabled != 3"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="码状态" prop="enabled">
            <el-radio-group v-model="form.enabled">
              <el-radio
                v-for="dict in bus_enabled"
                :key="dict.value"
                :value="dict.value"
              >{{ dict.label }}</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col> -->
      </el-row>
      <el-form-item label="码方案描述" prop="remark" >
        <el-input type="textarea" v-model="form.remark" :rows="4" :maxlength="100" show-word-limit placeholder="请输入内容，不超过100个字" />
      </el-form-item>
    </el-form>
    <div class="dialog-footer">
      <el-button @click="onCancel">取消</el-button>
      <el-button type="primary" @click="onConfirm">确认</el-button>
    </div>
  </div>
</template>

<script setup>
import { getPlatCodeManagement, addPlatCodeManagement, updatePlatCodeManagement } from "@/api/bus/PlatCodeManagement"
import { getCaList } from '@/api/bus/certificateManagement'

const { proxy } = getCurrentInstance()
const { bus_qr_code_type,bus_enabled,bus_qr_code_standard } = proxy.useDict('bus_qr_code_type','bus_enabled','bus_qr_code_standard')

const emits = defineEmits(['cancel', 'confirm'])

const caList = ref([])
const PlatCodeManagementRef = ref()
const form = ref({
  id: null,
  caId: null,
  codeName: null,
  codeLabel: null,
  codeType: null,
  codeStandard: null,
  codeOrgan: null,
  enabled: '1',
  codeFlag: null,
  createBy: null,
  createTime: null,
  updateBy: null,
  updateTime: null
})
const rules = {
  codeName: [{ required: true, message: "码方案名称不能为空", trigger: "blur" }],
  codeLabel: [
    { required: true, message: "码方案code不能为空", trigger: "blur" },
    { pattern: /^[A-Za-z0-9_]+$/, message: "码方案code格式不正确", trigger: "blur" }
  ],
  codeType: [{ required: true, message: "请选择码类别", trigger: "blur" }],
  codeStandard: [{ required: true, message: "请选择码标准", trigger: "blur" }],
  codeOrgan: [{ required: true, message: "发码机构不能为空", trigger: "blur" }],
  caId: [{ required: true, message: "请选择公钥证书", trigger: "blur" }],
  enabled: [{ required: true, message: "请选择码状态", trigger: "blur" }],
}

const pageInit = (id) => {
  getCaList().then((response) => {
		caList.value = response.data || []
	})
  if (id) {
    getPlatCodeManagement(id).then(response => {
      form.value = response.data
      form.value.enabled = form.value.enabled.toString()
    })
  }
}

const onCancel = () => {
  emits('cancel')
}

const onConfirm = () => {
  PlatCodeManagementRef.value.validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updatePlatCodeManagement(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          PlatCodeManagementRef.value.resetFields()
          emits('confirm')
        })
      } else {
        addPlatCodeManagement(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          PlatCodeManagementRef.value.resetFields()
          emits('confirm')
        })
      }
    }
  })
}

defineExpose({
  pageInit
})

</script>

<style lang="scss" scoped>

</style>